﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.RadicadosAnulados;
using System.Web.UI.WebControls;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Collections.Generic;

using System.Collections;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class RadicadosAnulados : BaseApplicationPage, IRadicadosAnuladosView
    {
        #region fields

        private RadicadosAnuladosPresenter _presenter;

        #endregion

        #region Properties

        public List<Radicado> foundItems { get; set; }

        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }

        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new RadicadosAnuladosPresenter(this);
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            //pgrTemplates.NextLinkText = ">>";
            //pgrTemplates.PreviousLinkText = "<<";
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
            pgrTemplates.BuildPager();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack) return;

            _presenter.HandleLoad();
            pgrTemplates.BuildPager(); 
        }
        protected void gvAnulados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                e.Row.Cells[0].Visible = false;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    HyperLink lblRadicado = e.Row.FindControl("HyperLinkID") as HyperLink;
                    if (lblRadicado != null)
                    {
                        lblRadicado.NavigateUrl = SPContext.Current.Web.Url+"/" + geturlActa(e.Row.Cells[0].Text); 
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandleError(ex);
            }
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            _presenter.HandleLoad();
            pgrTemplates.BuildPager();
        }

        private string geturlActa(string Id)
        {
            string result = string.Empty;

            SPList listaAnulados = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_ANULADOS);
            if (listaAnulados != null)
            {
                SPListItem currentItem = listaAnulados.GetItemById(int.Parse(Id));
                SPList listaActas = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_ACTAS_DE_ANULACION);
                if (currentItem[CustomSiteColumns.FIELD_DOCUMENT_ACTA_ID] != null)
                {
                    SPListItem ActaItem = listaActas.GetItemById(int.Parse(currentItem[CustomSiteColumns.FIELD_DOCUMENT_ACTA_ID].ToString()));
                    result = ActaItem.Url;
                }
            }

            return result;
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvAnulados.DataSource = foundItems;
            gvAnulados.DataBind();
        }

        #endregion
    }
}
